package com.i2silly.wxbot.listener;

import com.i2silly.wxbot.config.RobotConfiguration;
import com.i2silly.wxbot.config.TempCache;
import com.i2silly.wxbot.constant.WxRobotConstant;
import com.i2silly.wxbot.entity.BotChatroom;
import com.i2silly.wxbot.entity.BotContact;
import com.i2silly.wxbot.pojo.WsReceive;
import com.i2silly.wxbot.quartz.QuartzJobService;
import com.i2silly.wxbot.quartz.job.GetChatroomJob;
import com.i2silly.wxbot.quartz.job.RemoveChatJob;
import com.i2silly.wxbot.quartz.job.WebsocketReconnJob;
import com.i2silly.wxbot.server.RobotWebsocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @Title: SpringBootListener
 * @description: 初始化
 * @author: i2silly
 * @since: 2020年08月21日 15:35
 */
@Component
public class WxRobotListener implements ServletContextListener {

    private final Logger logger = LoggerFactory.getLogger(WxRobotListener.class);

    @Autowired
    private RobotConfiguration robot;

    @Autowired
    private QuartzJobService service;

    @Autowired
    private RobotWebsocketServer  server;

    @Autowired
    private TempCache cache;

    @Override
    public void contextInitialized(ServletContextEvent sce){
        // 监听内容 coding
        server.connect();
        service.addJob(WebsocketReconnJob.class.getName(), WebsocketReconnJob.class, WxRobotConstant.CRON_TWO_MIN);
        // 执行获取群成员列表 半小时一次
        service.addJob(GetChatroomJob.class.getName(), GetChatroomJob.class, WxRobotConstant.CRON_HALF_HOUR);
        // 执行移出上次聊天列表
        service.addJob(RemoveChatJob.class.getName(), RemoveChatJob.class, WxRobotConstant.CRON_ONE_MIN);
        // 初次启动缓存设置值
        cache.addCache(WxRobotConstant.ROBOT_INIT, false);
        cache.addCache(WxRobotConstant.CONTACT_INIT, false);
        cache.addCache(WxRobotConstant.CHATROOM_INIT, false);
        // 联系人缓存
        cache.addCache(WxRobotConstant.CONTACT_CACHE, new HashMap<String, BotContact>());
        // 群缓存
        cache.addCache(WxRobotConstant.CHATROOM_CACHE,new HashMap<String, List<BotChatroom>>());
        // 监听微信缓存
        cache.addCache(WxRobotConstant.MONITOR_WXID_CACHE, new HashSet<String>());
        // 获取上次接收缓存
        cache.addCache(WxRobotConstant.LAST_RECEIVE_MSG, new HashMap<String, WsReceive>());
    }
}
